package ui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.Timer;
import javax.swing.table.DefaultTableModel;

import util.Java_JDBC;
import config.Config;
import config.PaintConfig;

public class ShowSignUI extends JPanel {
	private int width, hight, signX, signY, noSignX, noSignY, unitHeight, gWidth, border;
	JLabel jLabel1, jLabel2;
	private int signHeight;
	private int noSignHeight;
	private JFrame frame;
	private JTable table;
	private Object[][] data;
	private String[] colname = {"签到班级","姓名","签到时间"};
	private JScrollPane jsPane;//表格

	private static String[] signState = {"实时签到", "班级签到信息"};
	public ShowSignUI() {
		frame = new JFrame();
		width = PaintConfig.width;
		hight = PaintConfig.hight;
		gWidth = PaintConfig.gWidth;
		getXY();
		// 设置布局
		this.setLayout(null);
		
		final JComboBox<Object> ca = new JComboBox<Object>(signState);
		ca.setBounds(230, 0, 140, 30);
		ca.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Config.SelectedSignItem = (String) ca.getSelectedItem();
				System.out.println("selected:   "+Config.SelectedSignItem);
			}
		});
		ca.setSelectedIndex(0);
		
		jLabel1 = new JLabel();
		jLabel2 = new JLabel();
		jLabel1.setBounds(signX, 10, width / 2, 50);
		jLabel2.setBounds(noSignX, 10, width / 2, 50);
		
		createTableData();  //创建表格所需数据 
        table = getSimpleTable();  
        jsPane = new JScrollPane(table);
        jsPane.setBounds(10, 100,width-20,hight-50);
        this.add(jsPane);
        jsPane.setVisible(false);
        
		this.add(jLabel1);
		this.add(jLabel2);
		this.add(ca);
		this.setBounds(0, 0, width, hight);

		frame.getContentPane().add(this);
//		 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(width, hight);
		frame.setLocationRelativeTo(null);
		frame.setResizable(false);
		frame.setTitle("签到情况");
		frame.setVisible(true);
		
		ActionListener animate = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				repaint();
			}
        };
        Timer timer = new Timer(500,animate);
        timer.start();
	}

	@Override
	public void paintComponent(final Graphics g) {
		super.paintComponent(g);
		getXY();
		if(Config.SelectedSignItem.equals("实时签到")) {
			g.setColor(Color.BLUE);
			g.fillRect(signX, signY, gWidth, signHeight);
			g.setColor(Color.RED);
			g.fillRect(noSignX, noSignY, gWidth, noSignHeight);
			jLabel1.setText("已签到人数：" + Config.signNumber);
			jLabel2.setText("未签到人数：" + (Config.totalNumber - Config.signNumber));
			jLabel1.setVisible(true);
			jLabel2.setVisible(true);
			jsPane.setVisible(false);
		}else if(Config.SelectedSignItem.equals("班级签到信息")) {
			try {
				List<ArrayList<String>> result = new Java_JDBC().searchClassSigned(Config.className);
				if(result.size()>10) {
					DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
					tableModel.setColumnCount(result.size());
				}
				for(int i=0;i<result.size();i++) {
					for(int j=0;j<result.get(i).size();j++) {
						table.setValueAt(result.get(i).get(j), i, j);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			jLabel1.setVisible(false);
			jLabel2.setVisible(false);
			jsPane.setVisible(true);
		}
	}

	/**
	 * 根据签到人数和总人数，得到签到矩形左上角坐标
	 * 
	 * @param signNumber
	 * @param totalNumber
	 */
	private void getXY() {
		border = width / 8;
		if(Config.totalNumber!=0) {
			unitHeight = (hight - border * 2) / Config.totalNumber;
		}
		signHeight = Config.signNumber * unitHeight;
		signY = hight - signHeight - border;
		signX = border;
		noSignHeight = (Config.totalNumber - Config.signNumber) * unitHeight;
		noSignY = hight - noSignHeight - border;
		noSignX = width - border - gWidth;
		System.out.println("签到人数："+Config.signNumber);
	}
	
	 /**
     * 返回一个简单的表格
     * @return
     */
    private JTable getSimpleTable(){
        table = new JTable(data,colname);
        table.setEnabled(false);
        return table;
    }
    
    /**
     * 给表格添加数据
     */
    private void createTableData(){
        data = new Object[10][3];
    }
	
//	public static void main(String[] args) {
//		new ShowSignUI().setVisible(true);
//	}
}
